import Vue from 'vue'
import {
  ACCESS_TOKEN,
  USER_INFO,
} from '@/store/mutation-types'

import {
  generateSaltRequest,
  getSaltRequest,
  sysLoginRequest,
  sysPhoneLoginRequest,
  getUserInfoRequest,
  sysLogoutRequest,
  sendCaptchaRequest,
  updateUserInfoRequest,
  getImageCaptchaRequest,
} from '@/api/user'

const user = {
  namespaced: true,
  state: {
    oldSalt: undefined,
    salt: undefined,
    token: '',
    captchaCheckKey: '',
    imageCaptcha: '',
    info: {},
  },

  mutations: {
    SET_OLD_SALT: (state, oldSalt) => {
      state.oldSalt = oldSalt
    },

    SET_SALT: (state, salt) => {
      state.salt = salt
    },

    SET_TOKEN: (state, token) => {
      state.token = token
    },

    SET_NAME: (state, { username, realname, welcome }) => {
      state.username = username
      state.realname = realname
      state.welcome = welcome
    },

    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },

    SET_PERMISSIONLIST: (state, permissionList) => {
      state.permissionList = permissionList
    },

    SET_INFO: (state, info) => {
      state.info = info
    },

    SET_TENANT: (state, id) => {
      state.tenantid = id
    },

    SET_CAPTCHA_CHECK_KEY: (state, captchaCheckKey) => {
      state.captchaCheckKey = captchaCheckKey
    },

    SET_IMAGE_CAPTCHA: (state, imageCaptcha) => {
      state.imageCaptcha = imageCaptcha
    },

  },

  actions: {
    generateSaltAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        generateSaltRequest(parameters)
          .then(response => {
            if (response && response.success && response.result) {
              const oldSalt = response.result.oldSalt
              const salt = response.result.salt
              if (salt) {
                commit('SET_SALT', salt)
              }
              if (oldSalt) {
                commit('SET_OLD_SALT', oldSalt)
              }
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    getSaltAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        getSaltRequest(parameters)
          .then(response => {
            if (response && response.success && response.result) {
              const salt = response.result.salt
              if (salt) {
                commit('SET_SALT', salt)
              }

              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },


    // 登录
    loginAction({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        sysLoginRequest(userInfo)
          .then(response => {
            if (response.code == 200) {
              const result = response.result
              console.info(
                `\n\n-------- login result Info Begin --------\n\n`,
                result,
                `\n\n-------- login result Info end   --------\n\n`
              );
              const userInfo = result.userInfo
              Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)

              commit('SET_TOKEN', result.token)
              commit('SET_INFO', userInfo)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },


    // 登录
    getUserInfoAction({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        getUserInfoRequest(userInfo)
          .then(response => {
            if (response.code == 200) {
              const result = response.result
              const userInfo = result.userInfo
              if (userInfo) {
                commit('SET_INFO', userInfo)
              }

              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 手机号登录
    phoneLoginAction({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        sysPhoneLoginRequest(userInfo)
          .then(response => {
            if (response.code == '200') {
              const result = response.result
              const userInfo = result.userInfo
              Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
              Vue.ls.set(USER_INFO, userInfo, 7 * 24 * 60 * 60 * 1000)
              commit('SET_TOKEN', result.token)
              commit('SET_INFO', userInfo)
              commit('SET_AVATAR', userInfo.avatar)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    getImageCaptchaAction({ commit }, parameters) {
      return new Promise((resolve, reject) => {
        const captchaCheckKey = new Date().getTime()
        commit('SET_CAPTCHA_CHECK_KEY', captchaCheckKey)
        getImageCaptchaRequest(captchaCheckKey)
          .then(response => {
            if (response && response.success) {
              const imageCaptcha = response.result
              commit('SET_IMAGE_CAPTCHA', imageCaptcha)
              resolve(response)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            console.info(
              `\n\n-------- error Info Begin --------\n\n`,
              error,
              `\n\n-------- error Info end   --------\n\n`
            );
            reject(error)
          })
      })
    },

    // 登出
    logoutAction({ commit, state }) {
      // return new Promise(resolve => {
      //   let logoutToken = state.token
      //   commit('SET_TOKEN', '')
      //   commit('SET_PERMISSIONLIST', [])
      //   Vue.ls.remove(ACCESS_TOKEN)
      //   Vue.ls.remove(USER_INFO)
      //   Vue.ls.remove(USER_NAME)
      //   Vue.ls.remove(UI_CACHE_DB_DICT_DATA)
      //   Vue.ls.remove(CACHE_INCLUDED_ROUTES)
      //   // console.log('logoutToken: '+ logoutToken)
      //   logout(logoutToken)
      //     .then(() => {
      //       if (process.env.VUE_APP_SSO == 'true') {
      //         let sevice = 'http://' + window.location.host + '/'
      //         let serviceUrl = encodeURIComponent(sevice)
      //         window.location.href = process.env.VUE_APP_CAS_BASE_URL + '/logout?service=' + serviceUrl
      //       }
      //       resolve()
      //     })
      //     .catch(() => {
      //       resolve()
      //     })
      // })
    },

  }
}

export default user

